// index.js
Page({
  data: {
    userInfo: {
      avatarUrl: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
      nickName: ''
    },
    dayArr: ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"],
    daysInMonth: [],
    monthAr: [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
    year: new Date().getFullYear(),
    month: new Date().getMonth(),
    consistentRecordDay: 0
  },

  onLoad(options) {
    this.calculateDaysInMonth();
    const userInfo = getApp().globalData.userInfo;
    this.setData({
      userInfo: userInfo || this.data.userInfo
    });
    console.log('Global userInfo:', this.data.userInfo);
  },

  onLeftArrowClick() {
    wx.navigateBack({
      delta: 1
    });
  },

  handleImageError(event) {
    console.error('Image load failed:', event);
    this.setData({
      'userInfo.avatarUrl': '默认图片URL'
    });
  },

  calculateDaysInMonth() {
    const daysInMonth = this.data.monthAr[this.data.month];
    const firstDayOfWeek = new Date(this.data.year, this.data.month, 1).getDay();
    const daysBeforeFirstDay = firstDayOfWeek === 0 ? 6 : firstDayOfWeek - 1;
    let days = [];
    for (let i = 1; i <= daysInMonth; i++) {
      const dayOfWeek = (firstDayOfWeek + i - 1) % 7 || 7;
      days.push({
        date: i,
        day: this.data.dayArr[(dayOfWeek - 1 + 7) % 7],
        isRecord: Math.random() > 0.8
      });
    }
    for (let i = 0; i < daysBeforeFirstDay; i++) {
      days.unshift({
        date: 0,
        day: this.data.dayArr[(i + 1 + 7) % 7],
        isRecord: false
      });
    }
    this.setData({
      daysInMonth: days
    });
  }
});
